package com.ycsj.goldmedalnewconcept.activity;

import java.io.IOException;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;

import com.flyco.animation.BaseAnimatorSet;
import com.flyco.dialog.listener.OnBtnLeftClickL;
import com.flyco.dialog.listener.OnBtnRightClickL;
import com.flyco.dialog.widget.NormalDialog;
import com.google.gson.Gson;
import com.squareup.okhttp.Callback;
import com.squareup.okhttp.FormEncodingBuilder;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import com.squareup.picasso.Picasso;
import com.ycsj.goldmedalnewconcept.adapter.HuodongAdapter;
import com.ycsj.goldmedalnewconcept.adapter.MyPagerAdapter;
import com.ycsj.goldmedalnewconcept.bean.AttResponse;
import com.ycsj.goldmedalnewconcept.bean.GonggaoInfo;
import com.ycsj.goldmedalnewconcept.bean.HuodongResponse;
import com.ycsj.goldmedalnewconcept.constant.Constant;
import com.ycsj.goldmedalnewconcept.utils.Bimp;
import com.ycsj.goldmedalnewconcept.utils.SPUtil;
import com.ycsj.goldmedalnewconcept.view.PullToRefreshLayout;
import com.ycsj.goldmedalnewconcept.R;

//活动详情
public class HuodongDetailsActivity extends Activity implements OnClickListener {

	private ImageView iv_back;
	private Intent intent;
	private int position;
	private String role;
	private String account;
	private String address;
	private String begindate;
	private String enddate;
	private String content;
	private String start_status = "-1";
	private String pic1;
	private String pic2;
	private String pic3;
	private String pic4;
	private String pic5;
	private String pic6;
	private String pic7;
	private String pic8;
	private String pic9;
	private String enroll_status = "0";
	private TextView tv_tag;
	private TextView tv_address;
	private TextView tv_time;
	private TextView tv_content;
	private TextView tv_person;
	private TextView tv_button;

	private ViewPager mViewPager;
	private LinearLayout mLinearLayoutDot;
	private List<ImageView> mImageViewList;
	private int currentPosition = 0;
	private List<ImageView> mImageViewDotList;
	private int dotPosition = 0;
	private int prePosition = 0;
	private String tag;
	private String begin_date;
	private String end_date;
	private String persons;
	private String rowid;

	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_huodongdetails);
		role = SPUtil.getString(this, "role", "");
		account = SPUtil.getString(this, "account", "");

		
		intent = getIntent();
		tag = intent.getStringExtra("tag");
		begin_date = intent.getStringExtra("begin_date");
		end_date = intent.getStringExtra("end_date");
		address = intent.getStringExtra("address");
		content = intent.getStringExtra("content");
		persons = intent.getStringExtra("persons");
		pic1 = intent.getStringExtra("pic1");
		pic2 = intent.getStringExtra("pic2");
		pic3 = intent.getStringExtra("pic3");
		pic4 = intent.getStringExtra("pic4");
		pic5 = intent.getStringExtra("pic5");
		pic6 = intent.getStringExtra("pic6");
		pic7 = intent.getStringExtra("pic7");
		pic8 = intent.getStringExtra("pic8");
		pic9 = intent.getStringExtra("pic9");
		rowid = intent.getStringExtra("rowid");
		enroll_status = intent.getStringExtra("enroll_status");
		start_status = intent.getStringExtra("start_status");
		
		Log.e("rowid","rowid" + rowid);
		initView();
		setOnListener();
		initData();
		if (mImageViewList != null && mImageViewList.size() != 0) {
			setDot();
			setViewPager();
		}

		if (pic1==null||"".equals(pic1)) {
			ImageView iv = new ImageView(HuodongDetailsActivity.this);
			iv.setBackgroundResource(R.drawable.huodongdefault);
			mImageViewList.add(iv);
			MyPagerAdapter adapter = new MyPagerAdapter(mImageViewList);
			mViewPager.setAdapter(adapter);
		}
	}

	// 设置轮播小圆点
	private void setDot() {
		// 设置LinearLayout的子控件的宽高，这里单位是像素。
		LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(15, 15);
		params.rightMargin = 20;
		// for循环创建images.length个ImageView（小圆点）
		for (int i = 0; i < mImageViewList.size(); i++) {
			ImageView imageViewDot = new ImageView(this);
			imageViewDot.setLayoutParams(params);
			// 设置小圆点的背景为暗红图片
			imageViewDot.setBackgroundResource(R.drawable.red_dot_night);
			mLinearLayoutDot.addView(imageViewDot);
			mImageViewDotList.add(imageViewDot);
		}
		// 设置第一个小圆点图片背景为红色
		mImageViewDotList.get(dotPosition).setBackgroundResource(
				R.drawable.red_dot);
	}

	private void setViewPager() {
		MyPagerAdapter adapter = new MyPagerAdapter(mImageViewList);

		mViewPager.setAdapter(adapter);

		mViewPager.setCurrentItem(currentPosition);
		// 页面改变监听
		mViewPager
				.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
					@Override
					public void onPageScrolled(int position,
							float positionOffset, int positionOffsetPixels) {

					}

					@Override
					public void onPageSelected(int position) {
						currentPosition = position;
						dotPosition = position;
						mImageViewDotList
								.get(prePosition)
								.setBackgroundResource(R.drawable.red_dot_night);

						mImageViewDotList.get(dotPosition)
								.setBackgroundResource(R.drawable.red_dot);
						prePosition = dotPosition;

					}

					@Override
					public void onPageScrollStateChanged(int state) {
						// 当state为SCROLL_STATE_IDLE即没有滑动的状态时切换页面
						if (state == ViewPager.SCROLL_STATE_IDLE) {
							mViewPager.setCurrentItem(currentPosition, false);
						}
					}
				});
	}

	private void initView() {
		// TODO Auto-generated method stub
		iv_back = (ImageView) findViewById(R.id.iv_back);

		mViewPager = (ViewPager) findViewById(R.id.vp_main);
		mLinearLayoutDot = (LinearLayout) findViewById(R.id.ll_main_dot);
		tv_tag = (TextView) findViewById(R.id.tv_tag);
		tv_address = (TextView) findViewById(R.id.tv_address);
		tv_time = (TextView) findViewById(R.id.tv_time);
		tv_content = (TextView) findViewById(R.id.tv_content);
		tv_person = (TextView) findViewById(R.id.tv_person);
		tv_button = (TextView) findViewById(R.id.tv_button);
		if("3".equals(role)){
			tv_button.setVisibility(tv_button.GONE);
		}
		if("2".equals(role)){
			tv_button.setText("报名名单");
		}
		if("1".equals(role)){
			
			if("0".equals(start_status)){
				tv_button.setText("我要报名");
			}
			if("1".equals(start_status)){
				tv_button.setText("进行中");
				tv_button.setClickable(false);
			}
			if("2".equals(start_status)){
				tv_button.setText("已结束");
				tv_button.setClickable(false);
			}
			
			
			
		}
		if("1".equals(enroll_status)){
			tv_button.setText("已报名");
			tv_button.setClickable(false);
		}
	}

	private void initData() {
		
		tv_tag.setText(tag);
		tv_time.setText(begin_date + " 至 " + end_date);
		tv_address.setText(address);
		tv_content.setText(content);
		tv_person.setText(persons);
		
		mImageViewList = new ArrayList<ImageView>();
		mImageViewDotList = new ArrayList();
		ImageView imageView = null;
		if(pic1!=null&&!"".equals(pic1)){
			imageView = new ImageView(HuodongDetailsActivity.this);
			imageView.setScaleType(ImageView.ScaleType.FIT_XY);
			Picasso.with(this).load(pic1)
			.into(imageView);
			mImageViewList.add(imageView);
		}
		if(pic2!=null&&!"".equals(pic2)){
			ImageView imageView2 = new ImageView(HuodongDetailsActivity.this);
			imageView2.setScaleType(ImageView.ScaleType.FIT_XY);
			Picasso.with(this).load(pic2)
			.into(imageView2);
			mImageViewList.add(imageView2);
		}if(pic3!=null&&!"".equals(pic3)){
			ImageView imageView3 = new ImageView(HuodongDetailsActivity.this);
			imageView3.setScaleType(ImageView.ScaleType.FIT_XY);
			Picasso.with(this).load(pic3)
			.into(imageView3);
			mImageViewList.add(imageView3);
		}
		if(pic4!=null&&!"".equals(pic4)){
			ImageView imageView4 = new ImageView(HuodongDetailsActivity.this);
			imageView4.setScaleType(ImageView.ScaleType.FIT_XY);
			Picasso.with(this).load(pic4)
			.into(imageView4);
			mImageViewList.add(imageView4);
		}
		if(pic5!=null&&!"".equals(pic5)){
			ImageView imageView5 = new ImageView(HuodongDetailsActivity.this);
			imageView5.setScaleType(ImageView.ScaleType.FIT_XY);
			Picasso.with(this).load(pic5)
			.into(imageView5);
			mImageViewList.add(imageView5);
		}
		if(pic6!=null&&!"".equals(pic6)){
			ImageView imageView6 = new ImageView(HuodongDetailsActivity.this);
			imageView6.setScaleType(ImageView.ScaleType.FIT_XY);
			Picasso.with(this).load(pic6)
			.into(imageView6);
			mImageViewList.add(imageView6);
		}
		if(pic7!=null&&!"".equals(pic7)){
			ImageView imageView7 = new ImageView(HuodongDetailsActivity.this);
			imageView7.setScaleType(ImageView.ScaleType.FIT_XY);
			Picasso.with(this).load(pic7)
			.into(imageView7);
			mImageViewList.add(imageView7);
		}
		if(pic8!=null&&!"".equals(pic8)){
			ImageView imageView8 = new ImageView(HuodongDetailsActivity.this);
			imageView8.setScaleType(ImageView.ScaleType.FIT_XY);
			Picasso.with(this).load(pic8)
			.into(imageView8);
			mImageViewList.add(imageView8);
		}
		if(pic9!=null&&!"".equals(pic9)){
			ImageView imageView9 = new ImageView(HuodongDetailsActivity.this);
			imageView9.setScaleType(ImageView.ScaleType.FIT_XY);
			Picasso.with(this).load(pic9)
			.into(imageView9);
			mImageViewList.add(imageView9);
		}

	}
	private void setOnListener() {
		// TODO Auto-generated method stub
		iv_back.setOnClickListener(this);
		if(!"1".equals(enroll_status)){
			if("0".equals(start_status)){
				tv_button.setOnClickListener(this);
			}
		}
		
		if("2".equals(role)){
			tv_button.setOnClickListener(this);
		}
	}
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.iv_back:
			finish();
			break;
		case R.id.tv_button:
			if("2".equals(role)){
				Intent intent2 = new Intent(HuodongDetailsActivity.this, HuodongMapActivity.class);
				intent2.putExtra("rowid2", rowid);
				Log.e("rowid","rowid" + rowid);
				startActivity(intent2);
			}
			if("1".equals(role)){
				deldialog(rowid);
			}
			break;
		}
	}
	private BaseAnimatorSet bas_in;
	private BaseAnimatorSet bas_out;

	public void setBasIn(
			BaseAnimatorSet bas_in) {
		this.bas_in = bas_in;
	}

	public void setBasOut(
			BaseAnimatorSet bas_out) {
		this.bas_out = bas_out;
	}
	private void deldialog(final String rowid) {
		
		
		
		final NormalDialog dialog = new NormalDialog(
				HuodongDetailsActivity.this);
		dialog.content("是否确认参加此次活动")
				.btnText("取消", "确定")//
				.style(NormalDialog.STYLE_TWO)//
				.titleTextSize(23)//
				.showAnim(bas_in)//
				.dismissAnim(
						bas_out)//
				.show();
		dialog.setCanceledOnTouchOutside(false);
		dialog.setOnBtnLeftClickL(new OnBtnLeftClickL() {

			@Override
			public void onBtnLeftClick() {
				dialog.dismiss();
			}
		});
		dialog.setOnBtnRightClickL(new OnBtnRightClickL() {

			@Override
			public void onBtnRightClick() {
				dialog.dismiss();
				
				OkHttpClient mOkHttpClient = new OkHttpClient();
				FormEncodingBuilder builder = new FormEncodingBuilder();
				builder.add("type", "14");
				builder.add("rowid2",rowid);
				builder.add("c", account);
				builder.add("role", role);
				builder.add("status", "0");
				
				Request request = new Request.Builder()
						.url(Constant.SERVER_URL + "PersonalAssistantMsg").post(builder.build())
						.build();
				mOkHttpClient.newCall(request).enqueue(new Callback() {

					@Override
					public void onFailure(Request arg0, IOException arg1) {
						runOnUiThread(new Runnable() {
							@Override
							public void run() {
								Toast.makeText(getBaseContext(), "网络无响应，请检查网络连接", 0)
										.show();
							}
						});
					}

					@Override
					public void onResponse(Response arg0) throws IOException {
						// 解析Json数据，保存登录状态及身份信息

						Gson gson = new Gson();
						String json = arg0.body().string();
						Log.e("test", json);
						AttResponse fromJson = gson.fromJson(json,
								AttResponse.class);

						if (fromJson != null) {
							if ("-1".equals(fromJson.state)
									|| "0".equals(fromJson.state)) {
								runOnUiThread(new Runnable() {
									@Override
									public void run() {
										Toast.makeText(getBaseContext(), "报名失败，请检查网络连接是否正常", 0)
										.show();
									}
								});
							} else if ("1".equals(fromJson.state)) {
								
								runOnUiThread(new Runnable() {
									@Override
									public void run() {
										Toast.makeText(getBaseContext(), "报名成功！", 0)
										.show();
											tv_button.setText("已报名");
											tv_button.setClickable(false);
											tv_person.setText(Integer.parseInt(persons)+1+"");
									}
								});
							}
						}
					}
				});
			}
		});
	}
}
